'use strict';

const Controller = require('egg').Controller;


const formateTree = treedata =>{
  return treedata.map(el=>{
    let obj = {
      id:el.deptId,
      label:el.deptName
    };
    if(el.children instanceof Array) {
      obj.children = formateTree(el.children);
    }
    return obj
  })
}

class DeptController extends Controller {
  async roleDeptTreeselect() {
    this.ctx.body = {code:200};
    const {ctx} = this;
    const {id} = ctx.params;
    const role = await ctx.model.SysRole.findOne({
      where:{
        roleId:Number(id)
      },
      include:[ctx.model.SysDept]
    });
    const checkedKeys = role.sys_depts.map(el=>el.toJSON()).map(el=>el.deptId);
    let list = await ctx.model.SysDept.findAll();
    list = list.map(el=>el.toJSON());
    const treedata = await ctx.helper.handleTree(list,"deptId","parentId");
    const depts = formateTree(treedata);
    ctx.body = {
      code:200,
      msg:"操作成功",
      depts,
      checkedKeys
    }
  }
  async treeselect(){
    const {ctx} = this;
    let list = await ctx.model.SysDept.findAll();
    list = list.map(el=>el.toJSON());
    const treedata = await ctx.helper.handleTree(list,"deptId","parentId");
    const depts = formateTree(treedata);
    ctx.body = ctx.helper.formateResponse(depts);
  }
  async index(){
    const {ctx,app} = this;
    const query = ctx.query;
    const {Op} = app.Sequelize;
    let params = {
      where:{},
      order:app.Sequelize.col("orderNum")
    }
    if(query.deptName){
      params.where.deptName = {[Op.like] : "%"+query.deptName+"%"}
    }
    if(query.status){
      params.where.status = query.status;
    }
    const list = await ctx.model.SysDept.findAll(params);
    ctx.body = ctx.helper.formateResponse(list);
  }
  async exclude(){
    const {ctx} = this;
    const depts = await ctx.model.SysDept.findAll();
    ctx.body = ctx.helper.formateResponse(depts);
  }
  async show(){
    const {ctx} = this;
    const {id} = ctx.params;
    let dept = await ctx.model.SysDept.findByPk(id);
    ctx.body = ctx.helper.formateResponse(dept);
  }
  async update(){
    const {ctx} = this;
    const data = ctx.request.body;
    let dept = await ctx.model.SysDept.findByPk(Number(data.deptId));
    await dept.update({
      deptName:data.deptName,
      email:data.email,
      leader:data.leader,
      orderNum:data.orderNum,
      parentId:data.parentId,
      phone:data.phone,
      status:data.status
    });
    ctx.body = ctx.helper.formateResponse(null);
  }
  async create(){
    const {ctx} = this;
    const data = ctx.request.body;
    let deptV = {
      deptName:data.deptName,
      email:data.email,
      leader:data.leader,
      orderNum:data.orderNum,
      parentId:data.parentId,
      phone:data.phone,
      status:data.status
    }
    await ctx.model.SysDept.create(deptV);
    ctx.body = ctx.helper.formateResponse(null);
  }
  
  async destroy(){
    const {ctx} = this;
    const {id} = ctx.params;
    const dept = await ctx.model.SysDept.findByPk(Number(id));
    await dept.destroy();
    ctx.body = ctx.helper.formateResponse(null);
  }
}

module.exports = DeptController;
